const config = require('../../common/config');
const util = require('../../utils/util');

let pageStart = -1;

Page({
    data: {
        inputShowed: false,
        inputVal: "",
        hotSearchShop: false,
        hotSearchWord: false,
        hotWords: [],
        hotShops: [],
        searching:false,
        shopList:[],
        showShopList:false
    },
    onLoad:function () {
        wx.setNavigationBarTitle({
            title: '搜一搜'
        })
       this.getHotSearchWord();
       this.getHotSearchShop();
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false,
            searching:false
        });
    },
    clearInput: function () {
        this.setData({
            inputVal: "",
            searching:false
        });

    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
        this.doSearch(e.detail.value)

    },
    getHotSearchWord:function () {
        var _this  = this;
        wx.showLoading({
            title: '加载中..',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask:true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'wxShopManageService',
                methodName: 'getHotSearchList',
                reqData:JSON.stringify({
                    platformId:'101',
                    userId:wx.getStorageSync(config.USER_ID),
                    theme:['02']
                })
            },
            success: function (res) {
                var resultData = res.data;
                if(resultData.code == 1){
                    var hotWords = resultData.data.hotSearchList;
                    util.debug(hotWords);
                    if(hotWords!=null&& hotWords.length>0){
                        _this.setData({
                            hotWords:hotWords,
                            hotSearchWord:true
                        })
                    }else {
                        _this.setData({
                            hotWords:[],
                            hotSearchWord:false
                        })
                    }
                }
            },
            fail:function (e) {
                util.debug(e.errMsg);
            },
            complete:function () {
                wx.hideLoading();
            }
        })
    },
    getHotSearchShop:function () {
        pageStart++;
        var _this  = this;
        wx.showLoading({
            title: '加载中..',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask:true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'wxShopManageService',
                methodName: 'getHotSearchList',
                reqData:JSON.stringify({
                    platformId:'101',
                    userId:wx.getStorageSync(config.USER_ID),
                    theme:['01'],
                    pageStart:0
                })
            },
            success: function (res) {
                var resultData = res.data;
                if(resultData.code == 1){
                    var hotShops = resultData.data.hotSearchList;
                    util.debug(hotShops);
                    if(hotShops!=null&&hotShops.length>0){
                        _this.setData({
                            hotShops:hotShops,
                            hotSearchShop:true
                        })
                    }else {
                        _this.setData({
                            hotShops:[],
                            hotSearchShop:false
                        })
                    }
                }
            },
            fail:function (e) {
                util.debug(e.errMsg);
            },
            complete:function () {
                wx.hideLoading();
            }
        })
    },
    doSearch:function (keywords) {
        var _this = this;
        util.debug("搜索关键词："+keywords)
        if(keywords.length>1 && !this.data.searching){
            this.setData({
                searching:true
            })
            wx.request({
                url: config.COMMPT_REQPATH,
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method:'POST',
                data: {
                    accessToken: '',
                    refreshToken: '',
                    serviceName: 'wxShopManageService',
                    methodName: 'getShopListFuzzy',
                    reqData:JSON.stringify({
                        keywords:keywords
                    })
                },
                success: function (res) {
                    var resultData = res.data;
                    if(resultData.code == 1){
                        var shopList = resultData.data.shopList;
                        if(shopList!=null&&shopList.length>0){
                         _this.setData({
                             showShopList:true,
                             shopList:shopList,
                             hotSearchShop:false
                         })
                        }else {
                            _this.setData({
                                showShopList:false,
                                shopList:[],
                                hotSearchShop:true
                            })
                        }

                    }else{
                        util.debug(resultData);
                    }
                },
                fail:function (e) {
                    util.debug(e.errMsg);
                },
                complete:function () {
                    _this.setData({
                        searching:false
                    })
                }
            })
        }
    },
    shopSearch:function (e) {
        this.doSearch(e.currentTarget.dataset.word)
    }

});